#include <iostream>
#include "Lock.hpp"
#include <string>
#include <filesystem>
#include <fstream>
#include <unistd.h>
class LogStrategy
{
public:
    LogStrategy()
    {
    }
    ~LogStrategy() {}
    virtual void Strategy(const std::string info);

private:
};

class ConsoleStrategy : public LogStrategy
{
public:
    ConsoleStrategy() {};
    ~ConsoleStrategy() {};
    void Strategy(const std::string info) override
    {
        LockGuard lock(_mutex);
        std::cout << info << std::endl;
    }

private:
    Mutex _mutex;
};

const std::string gsep = "/r/n";
const std::string defaultpath = "./log";
const std::string defaultfile = "my.log";
class FileLogStrategy : public LogStrategy
{
public:
    FileLogStrategy(const std::string path=defaultpath, const std::string file=defaultfile)
        : _path(path), _file(file)
    {
        _file_name = _path + (_path.back() == '/' ? "" : "/") + _file;
        if (std::filesystem::exists(_path))
        {
            return;
        }
        std::filesystem::create_directories(_path);
    }
    ~FileLogStrategy() {}
    void Strategy(const std::string info) override
    {
        LockGuard lock(_mutex);
        std::ofstream out(_file_name, std::ios::app);
        out << info << gsep;
        out.close();
    }

private:
    std::string _path;
    std::string _file;
    std::string _file_name;

    Mutex _mutex;
};

enum class LEVE
{
    DEBUG,
    INFO,
    WARNING,
    ERROR,
    FATAL,
};
std::string GetTime()
{
    time_t curr = time(nullptr);
    struct tm curr_tm;
    localtime_r(&curr, &curr_tm);
    char timebuffer[128];
    snprintf(timebuffer, sizeof(timebuffer), "%4d-%02d-%02d %02d:%02d:%02d",
             curr_tm.tm_year + 1900,
             curr_tm.tm_mon + 1,
             curr_tm.tm_mday,
             curr_tm.tm_hour,
             curr_tm.tm_min,
             curr_tm.tm_sec);
    return timebuffer;
}
std::string GetLeve(LEVE leve)
{
    switch (leve)
    {
    case LEVE::DEBUG:
        return "DEBUG";
    case LEVE::INFO:
        return "INFO";
    case LEVE::WARNING:
        return "WARNING";
    case LEVE::ERROR:
        return "ERROR";
    case LEVE::FATAL:
        return "FATAL";
    default:
        break;
    }
}

class logger
{
public:
    logger()
    {
        _flush_Stategy = std::make_unique<FileLogStrategy>();
    }
    class LogMessage
    {
    public:
        LogMessage(const std::string &src_file, const int &line, LEVE leve,logger& logg)
            : _time(GetTime()), _leve(GetLeve(leve)), _id(getpid()), _src_file(src_file), _line(line),_logg(logg)
        {
            std::stringstream ss;
            ss << "[" << _time << "] "
               << "[" << _leve << "] "
               << "[" << _id << "] "
               << "[" << _src_file << "] "
               << "[" << _line << "] "
               << "- ";
            _log_info = ss.str();
        }
        LogMessage &operator<<(const std::string message)
        {
            _log_info += message;
            return *this;
        }
        ~LogMessage()
        {
            _logg._flush_Stategy->Strategy(_log_info);
        }

    private:
        std::string _time;
        std::string _leve;
        pid_t _id;
        std::string _src_file;
        int _line;

        std::string _log_info;
        logger& _logg;
    };
    LogMessage operator()(const std::string &file, const int &line, LEVE level)
    {
        return LogMessage(file, line, level,*this);
    }
    ~logger()
    {
    }

private:
    std::unique_ptr<LogStrategy> _flush_Stategy;
};

#define LOG(level) logger(level, __FILE__, __LINE__)